package com.bst.base.controller;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.stream.Collectors;

import com.bst.common.vo.ExcelExportVO;
import com.bst.config.DownloadManager;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bst.common.annotation.Log;
import org.springframework.stereotype.Controller;
import com.bst.common.enums.BusinessType;
import com.bst.base.domain.BaseDownload;
import com.bst.base.service.IBaseDownloadService;
import com.bst.common.core.controller.BaseController;
import com.bst.common.core.domain.AjaxResult;
import com.bst.common.utils.poi.ExcelUtil;
import com.bst.common.core.page.TableDataInfo;

/**
 * 文件下载Controller
 * 
 * @author laoliu
 * @date 2025-02-26
 */
@Controller
@RequestMapping("/base/download")
public class BaseDownloadController extends BaseController
{
    private String prefix = "base/download";

    @Autowired
    private IBaseDownloadService baseDownloadService;

    @GetMapping()
    public String download(Long menuId, ModelMap mmap)
    {
        mmap.put("menuId",menuId);
        return prefix + "/download";
    }

    /**
     * 查询文件下载列表
     */
    @PostMapping("/queue/list")
    @ResponseBody
    public TableDataInfo list()
    {
        LinkedBlockingQueue<ExcelExportVO> originalQueue = DownloadManager.INSTANCE.getDownloadQueue();
        // 使用 Stream 记录元素
        Queue<ExcelExportVO> extractedElements = originalQueue.stream()
                .collect(Collectors.toCollection(LinkedList::new));
        List<ExcelExportVO> list = new ArrayList<>();
        list.addAll(extractedElements);
        return getDataTable(list);
    }
    /**
     * 查询文件下载列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BaseDownload baseDownload)
    {
        startPage();
        List<BaseDownload> list = baseDownloadService.selectBaseDownloadList(baseDownload);
        return getDataTable(list);
    }

    /**
     * 导出文件下载列表
     */
    @Log(title = "文件下载", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BaseDownload baseDownload)
    {
        List<BaseDownload> list = baseDownloadService.selectBaseDownloadList(baseDownload);
        ExcelUtil<BaseDownload> util = new ExcelUtil<BaseDownload>(BaseDownload.class);
        return util.exportExcel(list, "文件下载数据");
    }

    /**
     * 新增文件下载
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存文件下载
     */
    @Log(title = "文件下载", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BaseDownload baseDownload)
    {
        return toAjax(baseDownloadService.insertBaseDownload(baseDownload));
    }

    /**
     * 修改文件下载
     */
    @GetMapping("/edit/{idDownload}")
    public String edit(@PathVariable("idDownload") Long idDownload, ModelMap mmap)
    {
        BaseDownload baseDownload = baseDownloadService.selectBaseDownloadByIdDownload(idDownload);
        mmap.put("baseDownload", baseDownload);
        return prefix + "/edit";
    }

    /**
     * 修改保存文件下载
     */
    @Log(title = "文件下载", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BaseDownload baseDownload)
    {
        return toAjax(baseDownloadService.updateBaseDownload(baseDownload));
    }

    /**
     * 删除文件下载
     */
    @Log(title = "文件下载", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(baseDownloadService.deleteBaseDownloadByIdDownloads(ids));
    }
}
